package com.ting.test.algorithms.递归和动态规划.从左往右的模型;


import java.util.ArrayList;
import java.util.List;

public class 打印一个字符串的全部排列 {
    public static void main(String[] args) {
        List<String> ans = new ArrayList<>();
        String str = "sdfds";
        process(str, ans);
        for (String an : ans) {
            System.out.println(an);
        }
    }

    private static void process(String str, List<String> ans) {

//        if (StringUtils.isEmpty(str) || str.length() == 1) {
//            ans.add(str);
//        }
        List<Character> rest =new ArrayList<>();
        char[] chars = str.toCharArray();
        for (char c : chars) {
            rest.add(c);
        }

        func(rest, "", ans);
    }

    private static void func(List<Character> rest, String path, List<String> ans) {
        if (rest.isEmpty()) {
            ans.add(path);
        } else {
            for (int i = 0; i < rest.size(); i++) {
                Character currentChar = rest.remove(i);
                func(rest, path + currentChar, ans);
                rest.add(currentChar);//注意 这里恢复现场很重要
            }
        }
    }
}
